<?php

namespace AdminModule\Tools;

/**
 * Image
 *
 * @author David
 */
class Image {
    /**
     *
     * @var \Nette\Database\Connection
     */
    private $connection;
    
    public function __construct(\Nette\Database\Connection $connection) {
        $this->connection = $connection;
    }
    
    public function getPath($name, $type) {
        $config = $this->getConfig($type);
        if (!\is_object($config)) {
            return '';
        }        
        return $config->basepath . '/' . $name;
    }
    
    public function getThumbPath($name, $type) {
        $config = $this->getConfig($type);
        if (!\is_object($config)) {
            return '';
        }        
        return $config->basepath_thumb . '/' . $name;
    }
    
    public function getPathById($id) {
        $imageModel = new \AdminModule\Models\Image($this->connection);
        $image = $imageModel->get($id);
        if (!$image instanceof \Nette\Database\Table\ActiveRow) {
            return false;
        }
        return $this->getPath($image->name, $image->type);
    }
    
    public function getThumbPathById($id) {
        $imageModel = new \AdminModule\Models\Image($this->connection);
        $image = $imageModel->get($id);
        if (!$image instanceof \Nette\Database\Table\ActiveRow) {
            return false;
        }
        return $this->getThumbPath($image->name, $image->type);
    }
    
    private function getConfig($type) {
        $configModel = new \AdminModule\Models\ImageConfig($this->connection);
        return $configModel->getConfigByType($type);
    }
}
